clear all
set more off
capture log close
log using "$output\table7.log", replace

version 16		

use "$data/acsmergeddata.dta", clear

***************************************************************
*** number of kids
***************************************************************
gen numkids=min(newnumkids17,4)
assert numkids!=.

gen all=1
replace newnumkidswt17=round(newnumkidswt17,1)
bysort numkids: egen stnumkids=sum(newnumkidswt17)

gen poorkids=(poor==1)*newnumkidswt17
bysort numkids: egen stpoorkids=sum(poorkids)

gen stillpoor=(poor==1 & poorctc21==1)*newnumkidswt17
bysort numkids: egen ststillpoor=sum(stillpoor)


gen adultsstopwork=.
gen dynamicantipovkid=.

forvalues l=1/4{

* row 1
gen group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(numkids==`l')
gen group1=perwt*avgtax*.4*group
egen totgroup1a=sum(group1)

drop group*

* row 2
gen group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(numkids==`l')

gen group1=perwt*avgtax*.2*group
egen totgroup1b=sum(group1)

drop group*

* row 3
gen group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(numkids==`l')

gen group1=perwt*avgtax*.2*group
egen totgroup1c=sum(group1)

drop group*

* row 4
gen group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(numkids==`l')

gen group1=perwt*avgtax*.05*group
egen totgroup1d=sum(group1)

drop group*


* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(numkids==`l')
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)*(numkids==`l')

gen group1=(perwt*person1+spouseperwt*person2)*avgtax*.05
egen totgroup1e=sum(group1)

drop group*

replace adultsstopwork=totgroup1a+totgroup1b+totgroup1c+totgroup1d+totgroup1e if numkids==`l'
drop totgroup* person*

*** kids

gen group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(numkids==`l')

gen group4=newnumkidswt17*avgtax*.4*group*(poor==0)*poorctc21nother
egen totgroup4a=sum(group4)

gen group5=newnumkidswt17*avgtax*.4*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5a=sum(group5)

drop group*

* row 2
gen group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(numkids==`l')

gen group4=newnumkidswt17*avgtax*.2*group*(poor==0)*poorctc21nother
egen totgroup4b=sum(group4)

gen group5=newnumkidswt17*avgtax*.2*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5b=sum(group5)

drop group*

* row 3
gen group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(numkids==`l')

gen group4=newnumkidswt17*avgtax*.2*group*(poor==0)*poorctc21nother
egen totgroup4c=sum(group4)

gen group5=newnumkidswt17*avgtax*.2*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5c=sum(group5)

drop group*

* row 4
gen group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(numkids==`l')

gen group4=newnumkidswt17*avgtax*.05*group*(poor==0)*poorctc21notspouse
egen totgroup4d=sum(group4)

gen group5=newnumkidswt17*avgtax*.05*group*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5d=sum(group5)

drop group*


* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(numkids==`l')
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)*(numkids==`l')

gen group4=(newnumkidswt17*person1)*avgtax*.05*(poor==0)*poorctc21nother+(newnumkidswt17*person2)*avgtax*.05*(poor==0)*poorctc21notspouse
egen totgroup4e=sum(group4)

gen group5=(newnumkidswt17*person1)*avgtax*.05*(poor==1)*(poorctc21==0)*poorctc21nother+newnumkidswt17*person2*avgtax*.05*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5e=sum(group5)

drop group*

replace dynamicantipovkid=ststillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e if numkids==`l'

drop totgroup* person*

}	

gen poorrate1=100*stpoorkids/stnumkids
gen poorrate2=100*ststillpoor/stnumkids
gen poorrate3=100*dynamicantipovkid/stnumkids

gen povreduction=(poorrate1-poorrate3)/poorrate1

table numkid [fw=newnumkidswt17], c(n all mean faminc mean stpoorkids mean ststillpoor mean dynamicantipovkid ) 

table numkid [fw=newnumkidswt17], c(mean adultsstopwork mean poorrate1 mean poorrate2 mean poorrate3 mean povreduction)

drop poorrate* povreduction stpoorkids ststillpoor dynamicantipovkid stnumkids stpoorkids ststillpoor adultsstopwork numkid


***************************************************************
*** married
***************************************************************

bysort married: egen stnumkids=sum(newnumkidswt17)
bysort married: egen stpoorkids=sum(poorkids)
bysort married: egen ststillpoor=sum(stillpoor)
assert married!=.

gen adultsstopwork=.
gen dynamicantipovkid=.

forvalues l=0/1{

* row 1
gen group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(married==`l')
gen group1=perwt*avgtax*.4*group
egen totgroup1a=sum(group1)

drop group*

* row 2
gen group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(married==`l')

gen group1=perwt*avgtax*.2*group
egen totgroup1b=sum(group1)

drop group*

* row 3
gen group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(married==`l')

gen group1=perwt*avgtax*.2*group
egen totgroup1c=sum(group1)

drop group*

* row 4
gen group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(married==`l')

gen group1=perwt*avgtax*.05*group
egen totgroup1d=sum(group1)

drop group*


* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==`l')
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)*(married==`l')

gen group1=(perwt*person1+spouseperwt*person2)*avgtax*.05
egen totgroup1e=sum(group1)

drop group*

replace adultsstopwork=totgroup1a+totgroup1b+totgroup1c+totgroup1d+totgroup1e if married==`l'
drop totgroup* person*

*** kids

gen group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(married==`l')

gen group4=newnumkidswt17*avgtax*.4*group*(poor==0)*poorctc21nother
egen totgroup4a=sum(group4)

gen group5=newnumkidswt17*avgtax*.4*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5a=sum(group5)

drop group*

* row 2
gen group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(married==`l')

gen group4=newnumkidswt17*avgtax*.2*group*(poor==0)*poorctc21nother
egen totgroup4b=sum(group4)

gen group5=newnumkidswt17*avgtax*.2*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5b=sum(group5)

drop group*

* row 3
gen group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(married==`l')

gen group4=newnumkidswt17*avgtax*.2*group*(poor==0)*poorctc21nother
egen totgroup4c=sum(group4)

gen group5=newnumkidswt17*avgtax*.2*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5c=sum(group5)

drop group*

* row 4
gen group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(married==`l')

gen group4=newnumkidswt17*avgtax*.05*group*(poor==0)*poorctc21notspouse
egen totgroup4d=sum(group4)

gen group5=newnumkidswt17*avgtax*.05*group*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5d=sum(group5)

drop group*


* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==`l')
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)*(married==`l')

gen group4=(newnumkidswt17*person1)*avgtax*.05*(poor==0)*poorctc21nother+(newnumkidswt17*person2)*avgtax*.05*(poor==0)*poorctc21notspouse
egen totgroup4e=sum(group4)

gen group5=(newnumkidswt17*person1)*avgtax*.05*(poor==1)*(poorctc21==0)*poorctc21nother+newnumkidswt17*person2*avgtax*.05*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5e=sum(group5)

drop group*

foreach var in totgroup4a totgroup4b totgroup4c totgroup4d totgroup4e totgroup5a totgroup5b totgroup5c totgroup5d totgroup5e{
	replace `var'=0 if `var'==.
}

replace dynamicantipovkid=ststillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e if married==`l'

drop totgroup* person*

}	

gen poorrate1=100*stpoorkids/stnumkids
gen poorrate2=100*ststillpoor/stnumkids
gen poorrate3=100*dynamicantipovkid/stnumkids

gen povreduction=(poorrate1-poorrate3)/poorrate1

table married [fw=newnumkidswt17], c(n all mean faminc mean stpoorkids mean ststillpoor mean dynamicantipovkid ) 

table married [fw=newnumkidswt17], c(mean adultsstopwork mean poorrate1 mean poorrate2 mean poorrate3 mean povreduction)

drop poorrate* povreduction stpoorkids ststillpoor dynamicantipovkid stnumkids stpoorkids ststillpoor adultsstopwork



***************************************************************
*** race
***************************************************************

gen raceethnic=1 if race==1 & hispanic==0
replace raceethnic=2 if race==2 & hispanic==0
replace raceethnic=3 if race==3 & hispanic==0
replace raceethnic=4 if hispanic==1
replace raceethnic=5 if race>=4 & race<=6 & hispanic==0
replace raceethnic=6 if race>=7 & hispanic==0
assert raceethnic!=.
tab raceethnic,m

bysort raceethnic: egen stnumkids=sum(newnumkidswt17)
bysort raceethnic: egen stpoorkids=sum(poorkids)
bysort raceethnic: egen ststillpoor=sum(stillpoor)

gen adultsstopwork=.
gen dynamicantipovkid=.

forvalues l=1/6{

* row 1
gen group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(raceethnic==`l')
gen group1=perwt*avgtax*.4*group
egen totgroup1a=sum(group1)

drop group*

* row 2
gen group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(raceethnic==`l')

gen group1=perwt*avgtax*.2*group
egen totgroup1b=sum(group1)

drop group*

* row 3
gen group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(raceethnic==`l')

gen group1=perwt*avgtax*.2*group
egen totgroup1c=sum(group1)

drop group*

* row 4
gen group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(raceethnic==`l')

gen group1=perwt*avgtax*.05*group
egen totgroup1d=sum(group1)

drop group*


* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(raceethnic==`l')
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)*(raceethnic==`l')

gen group1=(perwt*person1+spouseperwt*person2)*avgtax*.05
egen totgroup1e=sum(group1)

drop group*

replace adultsstopwork=totgroup1a+totgroup1b+totgroup1c+totgroup1d+totgroup1e if raceethnic==`l'
drop totgroup* person*


*** kids

gen group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(raceethnic==`l')

gen group4=newnumkidswt17*avgtax*.4*group*(poor==0)*poorctc21nother
egen totgroup4a=sum(group4)

gen group5=newnumkidswt17*avgtax*.4*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5a=sum(group5)

drop group*

* row 2
gen group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(raceethnic==`l')

gen group4=newnumkidswt17*avgtax*.2*group*(poor==0)*poorctc21nother
egen totgroup4b=sum(group4)

gen group5=newnumkidswt17*avgtax*.2*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5b=sum(group5)

drop group*

* row 3
gen group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(raceethnic==`l')

gen group4=newnumkidswt17*avgtax*.2*group*(poor==0)*poorctc21nother
egen totgroup4c=sum(group4)

gen group5=newnumkidswt17*avgtax*.2*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5c=sum(group5)

drop group*

* row 4
gen group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(raceethnic==`l')

gen group4=newnumkidswt17*avgtax*.05*group*(poor==0)*poorctc21notspouse
egen totgroup4d=sum(group4)

gen group5=newnumkidswt17*avgtax*.05*group*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5d=sum(group5)

drop group*


* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(raceethnic==`l')
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)*(raceethnic==`l')

gen group4=(newnumkidswt17*person1)*avgtax*.05*(poor==0)*poorctc21nother+(newnumkidswt17*person2)*avgtax*.05*(poor==0)*poorctc21notspouse
egen totgroup4e=sum(group4)

gen group5=(newnumkidswt17*person1)*avgtax*.05*(poor==1)*(poorctc21==0)*poorctc21nother+newnumkidswt17*person2*avgtax*.05*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5e=sum(group5)

drop group*

foreach var in totgroup4a totgroup4b totgroup4c totgroup4d totgroup4e totgroup5a totgroup5b totgroup5c totgroup5d totgroup5e{
	replace `var'=0 if `var'==.
}

replace dynamicantipovkid=ststillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e if raceethnic==`l'

drop totgroup* person*
}	

gen poorrate1=100*stpoorkids/stnumkids
gen poorrate2=100*ststillpoor/stnumkids
gen poorrate3=100*dynamicantipovkid/stnumkids

gen povreduction=(poorrate1-poorrate3)/poorrate1

table raceethnic [fw=newnumkidswt17], c(n all mean faminc mean stpoorkids mean ststillpoor mean dynamicantipovkid ) 

table raceethnic [fw=newnumkidswt17], c(mean adultsstopwork mean poorrate1 mean poorrate2 mean poorrate3 mean povreduction)

drop poorrate* povreduction stpoorkids ststillpoor dynamicantipovkid stnumkids stpoorkids ststillpoor adultsstopwork raceethnic




***************************************************************
*** educ
***************************************************************

gen ed=1 if educ<=5
replace ed=2 if educ==6
replace ed=3 if educ==7 | educ==8
replace ed=4 if educ==10
replace ed=5 if educ==11
assert ed!=.
tab ed,m

bysort ed: egen stnumkids=sum(newnumkidswt17)
bysort ed: egen stpoorkids=sum(poorkids)
bysort ed: egen ststillpoor=sum(stillpoor)

gen adultsstopwork=.
gen dynamicantipovkid=.

forvalues l=1/6{

* row 1
gen group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(ed==`l')
gen group1=perwt*avgtax*.4*group
egen totgroup1a=sum(group1)

drop group*

* row 2
gen group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(ed==`l')

gen group1=perwt*avgtax*.2*group
egen totgroup1b=sum(group1)

drop group*

* row 3
gen group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(ed==`l')

gen group1=perwt*avgtax*.2*group
egen totgroup1c=sum(group1)

drop group*

* row 4
gen group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(ed==`l')

gen group1=perwt*avgtax*.05*group
egen totgroup1d=sum(group1)

drop group*


* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(ed==`l')
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)*(ed==`l')

gen group1=(perwt*person1+spouseperwt*person2)*avgtax*.05
egen totgroup1e=sum(group1)

drop group*

replace adultsstopwork=totgroup1a+totgroup1b+totgroup1c+totgroup1d+totgroup1e if ed==`l'
drop totgroup* person*


*** kids

gen group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(ed==`l')

gen group4=newnumkidswt17*avgtax*.4*group*(poor==0)*poorctc21nother
egen totgroup4a=sum(group4)

gen group5=newnumkidswt17*avgtax*.4*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5a=sum(group5)

drop group*

* row 2
gen group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(ed==`l')

gen group4=newnumkidswt17*avgtax*.2*group*(poor==0)*poorctc21nother
egen totgroup4b=sum(group4)

gen group5=newnumkidswt17*avgtax*.2*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5b=sum(group5)

drop group*

* row 3
gen group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(ed==`l')

gen group4=newnumkidswt17*avgtax*.2*group*(poor==0)*poorctc21nother
egen totgroup4c=sum(group4)

gen group5=newnumkidswt17*avgtax*.2*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5c=sum(group5)

drop group*

* row 4
gen group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(ed==`l')

gen group4=newnumkidswt17*avgtax*.05*group*(poor==0)*poorctc21notspouse
egen totgroup4d=sum(group4)

gen group5=newnumkidswt17*avgtax*.05*group*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5d=sum(group5)

drop group*


* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(ed==`l')
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)*(ed==`l')

gen group4=(newnumkidswt17*person1)*avgtax*.05*(poor==0)*poorctc21nother+(newnumkidswt17*person2)*avgtax*.05*(poor==0)*poorctc21notspouse
egen totgroup4e=sum(group4)

gen group5=(newnumkidswt17*person1)*avgtax*.05*(poor==1)*(poorctc21==0)*poorctc21nother+newnumkidswt17*person2*avgtax*.05*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5e=sum(group5)

drop group*

foreach var in totgroup4a totgroup4b totgroup4c totgroup4d totgroup4e totgroup5a totgroup5b totgroup5c totgroup5d totgroup5e{
	replace `var'=0 if `var'==.
}

replace dynamicantipovkid=ststillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e if ed==`l'

drop totgroup* person*
}	

gen poorrate1=100*stpoorkids/stnumkids
gen poorrate2=100*ststillpoor/stnumkids
gen poorrate3=100*dynamicantipovkid/stnumkids

gen povreduction=(poorrate1-poorrate3)/poorrate1

table ed [fw=newnumkidswt17], c(n all mean faminc mean stpoorkids mean ststillpoor mean dynamicantipovkid ) 

table ed [fw=newnumkidswt17], c(mean adultsstopwork mean poorrate1 mean poorrate2 mean poorrate3 mean povreduction)

drop poorrate* povreduction stpoorkids ststillpoor dynamicantipovkid stnumkids stpoorkids ststillpoor adultsstopwork ed




***************************************************************
*** urban/rural
***************************************************************

gen urbanrural=1 if metro==1
replace urbanrural=2 if metro==0
replace urbanrural=3 if metro==3
replace urbanrural=4 if metro==4
replace urbanrural=5 if metro==2
assert urbanrural!=.

tab urbanrural,m

bysort urbanrural: egen stnumkids=sum(newnumkidswt17)
bysort urbanrural: egen stpoorkids=sum(poorkids)
bysort urbanrural: egen ststillpoor=sum(stillpoor)

gen adultsstopwork=.
gen dynamicantipovkid=.

forvalues l=1/6{

* row 1
gen group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(urbanrural==`l')
gen group1=perwt*avgtax*.4*group
egen totgroup1a=sum(group1)

drop group*

* row 2
gen group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(urbanrural==`l')

gen group1=perwt*avgtax*.2*group
egen totgroup1b=sum(group1)

drop group*

* row 3
gen group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(urbanrural==`l')

gen group1=perwt*avgtax*.2*group
egen totgroup1c=sum(group1)

drop group*

* row 4
gen group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(urbanrural==`l')

gen group1=perwt*avgtax*.05*group
egen totgroup1d=sum(group1)

drop group*


* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(urbanrural==`l')
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)*(urbanrural==`l')

gen group1=(perwt*person1+spouseperwt*person2)*avgtax*.05
egen totgroup1e=sum(group1)

drop group*

replace adultsstopwork=totgroup1a+totgroup1b+totgroup1c+totgroup1d+totgroup1e if urbanrural==`l'
drop totgroup* person*


*** kids

gen group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(urbanrural==`l')

gen group4=newnumkidswt17*avgtax*.4*group*(poor==0)*poorctc21nother
egen totgroup4a=sum(group4)

gen group5=newnumkidswt17*avgtax*.4*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5a=sum(group5)

drop group*

* row 2
gen group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(urbanrural==`l')

gen group4=newnumkidswt17*avgtax*.2*group*(poor==0)*poorctc21nother
egen totgroup4b=sum(group4)

gen group5=newnumkidswt17*avgtax*.2*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5b=sum(group5)

drop group*

* row 3
gen group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(urbanrural==`l')

gen group4=newnumkidswt17*avgtax*.2*group*(poor==0)*poorctc21nother
egen totgroup4c=sum(group4)

gen group5=newnumkidswt17*avgtax*.2*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5c=sum(group5)

drop group*

* row 4
gen group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(urbanrural==`l')

gen group4=newnumkidswt17*avgtax*.05*group*(poor==0)*poorctc21notspouse
egen totgroup4d=sum(group4)

gen group5=newnumkidswt17*avgtax*.05*group*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5d=sum(group5)

drop group*


* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(urbanrural==`l')
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)*(urbanrural==`l')

gen group4=(newnumkidswt17*person1)*avgtax*.05*(poor==0)*poorctc21nother+(newnumkidswt17*person2)*avgtax*.05*(poor==0)*poorctc21notspouse
egen totgroup4e=sum(group4)

gen group5=(newnumkidswt17*person1)*avgtax*.05*(poor==1)*(poorctc21==0)*poorctc21nother+newnumkidswt17*person2*avgtax*.05*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5e=sum(group5)

drop group*

foreach var in totgroup4a totgroup4b totgroup4c totgroup4d totgroup4e totgroup5a totgroup5b totgroup5c totgroup5d totgroup5e{
	replace `var'=0 if `var'==.
}

replace dynamicantipovkid=ststillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e if urbanrural==`l'

drop totgroup* person*
}	

gen poorrate1=100*stpoorkids/stnumkids
gen poorrate2=100*ststillpoor/stnumkids
gen poorrate3=100*dynamicantipovkid/stnumkids

gen povreduction=(poorrate1-poorrate3)/poorrate1

table urbanrural [fw=newnumkidswt17], c(n all mean faminc mean stpoorkids mean ststillpoor mean dynamicantipovkid ) 

table urbanrural [fw=newnumkidswt17], c(mean adultsstopwork mean poorrate1 mean poorrate2 mean poorrate3 mean povreduction)

drop poorrate* povreduction stpoorkids ststillpoor dynamicantipovkid stnumkids stpoorkids ststillpoor adultsstopwork		


***************************************************************
*** renters/home owners
***************************************************************

gen rentvsown=1 if mortgage==0
replace rentvsown=2 if mortgage==3 | mortgage==4 | mortgage==1
*replace rentvsown=3 if mortgage==1

assert rentvsown!=.

tab rentvsown,m

bysort rentvsown: egen stnumkids=sum(newnumkidswt17)
bysort rentvsown: egen stpoorkids=sum(poorkids)
bysort rentvsown: egen ststillpoor=sum(stillpoor)

gen adultsstopwork=.
gen dynamicantipovkid=.

forvalues l=1/2{

* row 1
gen group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(rentvsown==`l')
gen group1=perwt*avgtax*.4*group
egen totgroup1a=sum(group1)

drop group*

* row 2
gen group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(rentvsown==`l')

gen group1=perwt*avgtax*.2*group
egen totgroup1b=sum(group1)

drop group*

* row 3
gen group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(rentvsown==`l')

gen group1=perwt*avgtax*.2*group
egen totgroup1c=sum(group1)

drop group*

* row 4
gen group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(rentvsown==`l')

gen group1=perwt*avgtax*.05*group
egen totgroup1d=sum(group1)

drop group*


* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(rentvsown==`l')
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)*(rentvsown==`l')

gen group1=(perwt*person1+spouseperwt*person2)*avgtax*.05
egen totgroup1e=sum(group1)

drop group*

replace adultsstopwork=totgroup1a+totgroup1b+totgroup1c+totgroup1d+totgroup1e if rentvsown==`l'
drop totgroup* person*


*** kids

gen group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(rentvsown==`l')

gen group4=newnumkidswt17*avgtax*.4*group*(poor==0)*poorctc21nother
egen totgroup4a=sum(group4)

gen group5=newnumkidswt17*avgtax*.4*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5a=sum(group5)

drop group*

* row 2
gen group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(rentvsown==`l')

gen group4=newnumkidswt17*avgtax*.2*group*(poor==0)*poorctc21nother
egen totgroup4b=sum(group4)

gen group5=newnumkidswt17*avgtax*.2*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5b=sum(group5)

drop group*

* row 3
gen group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(rentvsown==`l')

gen group4=newnumkidswt17*avgtax*.2*group*(poor==0)*poorctc21nother
egen totgroup4c=sum(group4)

gen group5=newnumkidswt17*avgtax*.2*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5c=sum(group5)

drop group*

* row 4
gen group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)*(rentvsown==`l')

gen group4=newnumkidswt17*avgtax*.05*group*(poor==0)*poorctc21notspouse
egen totgroup4d=sum(group4)

gen group5=newnumkidswt17*avgtax*.05*group*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5d=sum(group5)

drop group*


* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(rentvsown==`l')
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)*(rentvsown==`l')

gen group4=(newnumkidswt17*person1)*avgtax*.05*(poor==0)*poorctc21nother+(newnumkidswt17*person2)*avgtax*.05*(poor==0)*poorctc21notspouse
egen totgroup4e=sum(group4)

gen group5=(newnumkidswt17*person1)*avgtax*.05*(poor==1)*(poorctc21==0)*poorctc21nother+newnumkidswt17*person2*avgtax*.05*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5e=sum(group5)

drop group*

foreach var in totgroup4a totgroup4b totgroup4c totgroup4d totgroup4e totgroup5a totgroup5b totgroup5c totgroup5d totgroup5e{
	replace `var'=0 if `var'==.
}

replace dynamicantipovkid=ststillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e if rentvsown==`l'

drop totgroup* person*
}	

gen poorrate1=100*stpoorkids/stnumkids
gen poorrate2=100*ststillpoor/stnumkids
gen poorrate3=100*dynamicantipovkid/stnumkids

gen povreduction=(poorrate1-poorrate3)/poorrate1

table rentvsown [fw=newnumkidswt17], c(n all mean faminc mean stpoorkids mean ststillpoor mean dynamicantipovkid ) 

table rentvsown [fw=newnumkidswt17], c(mean adultsstopwork mean poorrate1 mean poorrate2 mean poorrate3 mean povreduction)

drop poorrate* povreduction stpoorkids ststillpoor dynamicantipovkid stnumkids stpoorkids ststillpoor adultsstopwork		

log close